package com.atguigu.tingshu.album.mapper;

import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface TrackInfoMapper extends BaseMapper<TrackInfo> {


    /**
     * 查看声音列表
     * @param trackListVoPage
     * @param trackInfoQuery
     * @return
     */
    IPage<TrackListVo> selectUserTrackPage(Page<TrackListVo> trackListVoPage, @Param("vo") TrackInfoQuery trackInfoQuery);

    /**
     * 修改order_num
     * @param albumId
     * @param orderNum
     */
    @Update("update track_info set order_num = order_num - 1 where album_id = #{albumId} and order_num > #{orderNum} and is_deleted = 0")
    void updateTrackInfoOrderNum(@Param("albumId") Long albumId, @Param("orderNum") Integer orderNum);

    /**
     * 根据专辑Id查询声音列表
     * @param albumId
     * @param listVoPage
     * @return
     */
    @Select("select\n" +
            "    track.id as trackId,\n" +
            "    track.track_title as trackTitle,\n" +
            "    track.media_duration as mediaDuration,\n" +
            "    track.order_num as orderNum,\n" +
            "    track.create_time as createTime,\n" +
            "    max(case when stat.stat_type='0701' then stat.stat_num else 0 end ) playStatNum,\n" +
            "    max(case when stat.stat_type='0704' then stat.stat_num else 0 end ) commentStatNum\n" +
            "    from track_info track inner join track_stat stat on track.id = stat.track_id\n" +
            "    where track.album_id = #{albumId}\n" +
            "group by track.id")
    IPage<AlbumTrackListVo> selectAlbumTrackPage(@Param("albumId") Long albumId, Page<AlbumTrackListVo> listVoPage);
}
